<?php

	/**
	 * @see Zend_Controller_Router_Rewrite
	 */
	require_once 'Zend/Controller/Router/Rewrite.php';
	
	
	class Bike_Controller_Router extends Zend_Controller_Router_Rewrite
	{
		private $_startingHandlers = array();

		public function addStartingHandlersInfo($HandlersInfo)
		{
			foreach($HandlersInfo as $name => $info)
			{
				$reflection = new Zend_Reflection_Class($info['class']);
				if(!$reflection->implementsInterface(Bike_Controller_Router_Handler_Interface))
				{
					Throw new Zend_Controller_Router_Exception($info['class'].' does not implement Bike_Controller_Router_Handler_Interface');
				}
				$handler = call_user_func(array($info['class'], 'getInstance'))->setName($name);
				$this->addStartingHandler($handler);
			}
		}

		public function addStartingHandler($Handler)
		{
			array_push($this->_startingHandlers, $Handler);
			return $this;
		}

		protected function getUriDelimiter()
		{
			return Zend_Controller_Router_Route::URI_DELIMITER;
		}

		protected function getPathInfoArr($Request)
		{
			$arr = explode($this->getUriDelimiter(), $Request->getPathInfo());
			if(!reset($arr))
			{
				array_shift($arr);
			}
			return $arr;
		}

		public function route(Zend_Controller_Request_Abstract $Request)
    	{
            $params = array();
            foreach($this->_startingHandlers as $Handler)
            {
            	$Handler->route($Request);
            	$params = array_merge($params, $Handler->getRouteParam());
            }
            $this->_setRequestParams($Request, $params);
            return parent::route($Request);
    	}

    	public function assemble($userParams, $name = null, $reset = false, $encode = true)
	    {
	        if (!is_array($userParams)) {
	            require_once 'Zend/Controller/Router/Exception.php';
	            throw new Zend_Controller_Router_Exception('userParams must be an array');
	        }

	        $urlParams = array();
    		foreach($this->_startingHandlers as $Handler)
            {
            	$result = $Handler->assemble($userParams);
            	unset($userParams[$Handler->getName()]);
            	if($result)
            	{
            		array_push($urlParams, $result);
            	}
            }

    		if ($name == null) {
	            try {
	                $name = $this->getCurrentRouteName();
	            } catch (Zend_Controller_Router_Exception $e) {
	                $name = 'default';
	            }
	        }

	        // Use UNION (+) in order to preserve numeric keys
	        $params = $userParams + $this->_globalParams;

	        $route = $this->getRoute($name);
	        $url   = $route->assemble($params, $reset, $encode);
	        if(sizeof($urlParams))
	        {
	        	$url = join($this->getUriDelimiter(), $urlParams).$this->getUriDelimiter().$url;
	      	}

	        if (!preg_match('|^[a-z]+://|', $url)) {
	            $url = rtrim($this->getFrontController()->getBaseUrl(), self::URI_DELIMITER) . self::URI_DELIMITER . $url;
	        }

	        return $url;
	    }
	}
